package com.ax.associator.shopassistant.controller;

import com.alibaba.fastjson.JSON;
import com.ax.associator.shopassistant.domain.BasePerseonnelDto;
import com.ax.associator.shopassistant.domain.BasePersonnel;
import com.ax.associator.shopassistant.domain.MenuDto;
import com.ax.associator.shopassistant.domain.MenuRoleDto;
import com.ax.associator.shopassistant.service.BasePersonnelService;
import com.ax.associator.store.common.ajax.AjaxRes;
import com.ax.associator.store.common.utils.Const;
import com.ax.associator.store.common.utils.PageUtils;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author Lg
 * @Date 2018/8/31 17:09
 * @Content
 */
@RestController
@RequestMapping("/basePersonnel/")
public class BasePersonnelController {

    @Autowired
    BasePersonnelService basePersonnelService;

    /**
     * 查询门店人员
     * @param pageUtils
     * @param basePersonnel
     * @return
     */
    @RequestMapping("queryPersonnel")
    public PageUtils queryPersonnel(@RequestBody Map<String,Object> map){
        PageUtils pageUtils = basePersonnelService.queryBasePersonnel(map);
        return pageUtils;
    }

    /**
     * 新增店员综合信息
     * @param ajaxRes
     * @param basePersonnel
     * @param headImgUrl
     * @return
     */
    @RequestMapping("insertInformation")
    public AjaxRes insertInformation(AjaxRes ajaxRes,@RequestBody BasePersonnel basePersonnel){
        try {
            Integer result = basePersonnelService.insertInformation(basePersonnel);
            ajaxRes.setSucceed(result, Const.OPERATE_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 启用禁用店员
     * @param ajaxRes
     * @param flag
     * @param personnelId
     * @return
     */
    @RequestMapping("updateFlag")
    public AjaxRes updateFlag(AjaxRes ajaxRes,@RequestBody Map<String,Object> map){
        try {
            String[] personnelId = map.get("personnelId").toString().split(",");
            Integer flag = Integer.valueOf(map.get("flag").toString());
            Integer result = basePersonnelService.updateFlag(flag,personnelId);
            ajaxRes.setSucceed(result, Const.OPERATE_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }


    /**
     * 删除店员
     * @param ajaxRes
     * @param personnelId
     * @return
     */
    @RequestMapping("deleteBasePersonnel")
    public AjaxRes deleteBasePersonnel(AjaxRes ajaxRes,@RequestBody Long personnelId){
        try {
            Integer result = basePersonnelService.deleteBasePersonnel(personnelId);
            ajaxRes.setSucceed(result, Const.OPERATE_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 查询药店店员名称，id，电话
     * @param ajaxRes
     * @return
     */
    @RequestMapping("queryDto")
    public AjaxRes queryDto(AjaxRes ajaxRes){
        try {
            //todo seesion取药店id
            Long drugstoreId = 1l;
            List<BasePerseonnelDto> result = basePersonnelService.queryDto(drugstoreId);
            ajaxRes.setSucceed(result, Const.OPERATE_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 查询菜单
     * @param ajaxRes
     * @return
     */
    @RequestMapping("queryMenuDto")
    public AjaxRes queryMenuDto(AjaxRes ajaxRes){
        try {
            List<MenuDto> result = basePersonnelService.queryMenuDto();
            ajaxRes.setSucceed(result,Const.OPERATE_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 新增用户菜单
     * @param ajaxRes
     * @param map
     * @return
     */
    @RequestMapping("insertPersonnelMenu")
    public AjaxRes insertPersonnelMenu(AjaxRes ajaxRes,@RequestBody Map<String,Object> map){
        try{
            Integer result = basePersonnelService.insertPersonnelMenu(map);
            ajaxRes.setSucceed(result,Const.OPERATE_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 查询店员角色菜单
     * @param ajaxRes
     * @param map
     * @return
     */
    @RequestMapping("queryMenuRole")
    public AjaxRes queryMenuRole(AjaxRes ajaxRes,@RequestBody Map<String,Object> map){
        try{
            Long drugstoreId =1L;
            String drugstoreName = "红梅药店";
            List<MenuRoleDto> result = basePersonnelService.queryMenuRole(drugstoreId,drugstoreName);
            ajaxRes.setSucceed(result,Const.OPERATE_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    @RequestMapping("updateMenuRole")
    public AjaxRes updateMenuRole(AjaxRes ajaxRes,@RequestBody Map<String,Object> map){
        return ajaxRes;
    }







}
